package cz.schovjan.proj.controllers;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.schovjan.proj.model.User;
import cz.schovjan.proj.model.UserRegistrFormBean;
import cz.schovjan.proj.service.IUser;

/**
 * Kontroler pro prehled vsech uzivatelu systemu a jejich detail.
 * @author schovjan
 *
 */
@Controller
@RequestMapping("/users")
@SessionAttributes({"userRegistrFormBean","user"}) //user se musi uvest taky, jinak se nepouzije
public class UserController extends Base {

	/** stranka s prehledem vsech uzivatelu */
	private static final String USERS = "users";
	
	/** stranka s detailum uzivatele */
	private static final String USER_DETAIL = "detailUser";
	
	/** Service pro uzivatele.  */
	@Autowired
	private IUser users;
	
	@ModelAttribute("userRegistrFormBean")
	public UserRegistrFormBean createUserRegistrFormBean() {
		
		return new UserRegistrFormBean();
	}
	
	
	/////////////////
	//OBSLUZNE FUKNCE ------------------------------------------------------------
	/////////////////
	
	
	/**
	 * Zobrazeni vsech uzivatelu systemu.
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/all")
	public String users(ModelMap model) {
		List<User> list = users.findAll();
		
		model.addAttribute("users", list);
		
		return USERS;
	}
	
	
	/**
	 * Zobrazeni editaci usera.
	 * @param nick
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/edit")
	public String edit(@ModelAttribute("user") User u, ModelMap model) {
		UserRegistrFormBean form = new UserRegistrFormBean();
		form.setNick(u.getNick());
		form.setName(u.getName());
		form.setSurname(u.getSurname());
		form.setPassword(u.getPassword());
		form.setPassword2(u.getPassword());
		form.setSign(u.getSign());
		form.setEmail(u.getEmail());
		
		model.addAttribute("userRegistrFormBean", form);
		
		
		return USER_DETAIL;
	}
	
	
	/**
	 * Vymazani uzivatele.
	 * @param nick
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delete")
	public String delete(@RequestParam("nick") String nick, ModelMap model) {
		users.delete(nick);
		
		return "redirect:/users/";
	}
	
	
	/**
	 * Registrace uzivatele
	 * @param form
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST, value="/userSave")
	public String userSave(@Valid UserRegistrFormBean form, BindingResult result, ModelMap model) {
		model.addAttribute("edit", true);
		
		if (!form.getPassword().equals(form.getPassword2())) {
			result.addError(new ObjectError("neshodaHesel", MSG_PASS_NOT_EQUAL));
		}
		
		if (result.hasErrors()) {
			model.addAttribute("login", false);
			return USER_DETAIL;
		}
		User u = users.save(
				form.getNick(),
				form.getPassword(),
				form.getName(),
				form.getSurname(),
				form.getSign(),
				form.getEmail()
			);
		model.addAttribute("user", u);
		model.addAttribute("message", MSG_USER_SAVE);

		return USER_DETAIL;
	}
	
}
